package com.hs.biye.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.mapper.DepartmentMapper;
import com.hs.biye.mapper.EmployeeMapper;
import com.hs.biye.mapper.PatientMapper;
import com.hs.biye.model.DTO.RoomRequest;
import com.hs.biye.model.VO.RoomVo;
import com.hs.biye.model.entity.Department;
import com.hs.biye.model.entity.Employee;
import com.hs.biye.model.entity.Patient;
import com.hs.biye.model.entity.Room;
import com.hs.biye.model.enums.RoomTypeEnum;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.RoomService;
import com.hs.biye.mapper.RoomMapper;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author haoshuai
 * @description 针对表【room】的数据库操作Service实现
 * @createDate 2024-01-18 12:18:20
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
        implements RoomService {

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private PatientMapper patientMapper;

    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 分页查询
     *
     * @param roomRequest
     * @return
     */
    public ResultPage<RoomVo> getPageRoom(RoomRequest roomRequest) {

        Integer page = roomRequest.getPages();
        Integer size = roomRequest.getSize();
        IPage<Room> page2 = new Page<>(page, size);
        LambdaQueryWrapper<Room> queryWrapper = getQueryWrapper(roomRequest);
        roomMapper.selectPage(page2, queryWrapper);

        List<Room> records = page2.getRecords();
        long total = page2.getTotal();
        long current = page2.getCurrent();
        long pages = page2.getPages();

        List<RoomVo> list = new ArrayList<>();
        records.forEach(item -> {
            RoomVo roomVo = new RoomVo();
            BeanUtils.copyProperties(item, roomVo);

            //获取医生姓名
            LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.select(Employee::getEmployeeName,Employee::getDepartmentId,Employee::getRoler)
                    .eq(Employee::getEmployeeId, item.getDoctorId()).eq(Employee::getRoler,"user");
            Employee employee = employeeMapper.selectOne(queryWrapper1);

            if (employee!=null){
                roomVo.setDoctorName(employee.getEmployeeName());
                LambdaQueryWrapper<Department> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.select(Department::getDepartmentName)
                        .eq(Department::getDepartmentId,employee.getDepartmentId());
                Department department = departmentMapper.selectOne(queryWrapper2);
                roomVo.setDepartmentName(department.getDepartmentName());

            }else {
                roomVo.setDoctorName("");
            }

            roomVo.setTypeName(RoomTypeEnum.getValueByCode(roomVo.getType()));

            list.add(roomVo);
        });
        return ResultPage.success(list, total, current, pages);
    }

    //查询单个
    @Override
    public RoomVo getRoomById(Integer id) {
        if (id == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Room room = getById(id);
        RoomVo roomVo = new RoomVo();
        BeanUtils.copyProperties(room, roomVo);

        LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.select(Employee::getEmployeeName,Employee::getDepartmentId)
                .eq(Employee::getEmployeeId, roomVo.getDoctorId());
        Employee employee = employeeMapper.selectOne(queryWrapper1);
        if (employee!=null){
            roomVo.setDoctorName(employee.getEmployeeName());

            LambdaQueryWrapper<Department> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.select(Department::getDepartmentName,Department::getDepartmentId)
                    .eq(Department::getDepartmentId,employee.getDepartmentId());
            Department department = departmentMapper.selectOne(queryWrapper2);
            roomVo.setDepartmentName(department.getDepartmentName());
            roomVo.setDepartmentId(department.getDepartmentId());
        }else {
            roomVo.setDoctorName("");
        }
        roomVo.setTypeName(RoomTypeEnum.getValueByCode(roomVo.getType()));
        return roomVo;
    }



    /**
     * 新增房间
     *
     * @param roomRequest
     * @return
     */
    @Override
    public Boolean addRoom(RoomRequest roomRequest) {
        if (roomRequest == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Room room = new Room();
        BeanUtils.copyProperties(roomRequest, room);
        return save(room);
    }


    /**
     * 修改
     * @param roomRequest
     * @return
     */
    public Boolean updateRoom(RoomRequest roomRequest) {
        if (roomRequest == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Room room = new Room();
        BeanUtils.copyProperties(roomRequest, room);
        return updateById(room);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Boolean deleteRoom(Integer id) {
        if (id == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        return removeById(id);
    }



    //获取病房的id和房间号
    @Override
    public List<RoomVo> getRoomByAndRoomNumber(Integer type) {
        if (type == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Room::getRoomId, Room::getRoomNumber).eq(Room::getType, type);
        List<Room> rooms = roomMapper.selectList(queryWrapper);

        //封装vo
        List<RoomVo> list = new ArrayList<>();
        rooms.forEach(item -> {
            RoomVo roomVo = new RoomVo();
            roomVo.setRoomId(item.getRoomId());
            roomVo.setRoomNumber(item.getRoomNumber());
            list.add(roomVo);
        });
        return list;
    }


    private LambdaQueryWrapper<Room> getQueryWrapper(RoomRequest roomRequest) {
        LambdaQueryWrapper<Room> employeeQueryWrapper = new LambdaQueryWrapper<>();
        Integer roomId = roomRequest.getRoomId();
        Integer type = roomRequest.getType();
        String doctorId = roomRequest.getDoctorId();
        String address = roomRequest.getAddress();
        employeeQueryWrapper.eq(type!=null, Room::getType, type);
        return employeeQueryWrapper;
    }
}




